<?php
class StrHelper {
	/**
	 * clean the html contente and return only the plain text
	 */
	public static function convertToPlainText($string){

		$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
				   '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
				   '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
				   '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA
		);
		$plainText = preg_replace($search, '', $string);

		return $plainText;
	}

	/**
	 * Clean user submited data safely
	 */
	public static function cleanUserSubmited($data)
	{
		if(is_array($data))	{
			$ret = array();
			foreach($data as $key=>$value)	{
				$ret[$key] = $this->cleanUserSubmited($value);
			}
			return $ret;
		} else	{
			if(!is_numeric($data)){
				if(get_magic_quotes_gpc()){
					$data = stripslashes($data);
				}
					
				//Escape string for database ; equivalant to mysql_real_escape_string
				$data = strtr($data, array(
					  "\x00" => '\x00',
					  "\n" => '\n', 
					  "\r" => '\r', 
					  '\\' => '\\\\',
					  "'" => "\'", 
					  '"' => '\"', 
					  "\x1a" => '\x1a'
					  ));
			}

			return $data;
		}
	}

	/**
	 * clear spaces and line break in texts
		*/
	public static function nl2blank($data){
		return preg_replace('#\r?\n#', '', $data);
	}


	/**
	 Find the nth position of char in string.
	  
	 */
	public static function strnpos($haystack, $needle, $occurance, $pos = 0) {
			
		$res = explode($needle,$haystack);
		$res = array_slice($res, $pos,  $occurance);
		return implode($needle,$res);
	}

	/**
	 * extend substr. Chinese words count with 2, and letters count with 1
	 * @param $string
	 * @param $length
	 * @param $etc
	 * @return string
	 */
	public static function zh_substr($string, $length = 20, $etc = '...')
	{
		$strcut = '';
		$strLength = 0;
		$width  = 0;
		$wordLength = 0;
		$string = strip_tags($string);
		$len = strlen($string);
		for($i = 0; $i < $len;  ) {
			if ( $width>=$length){
				break;
			}
			if( ord($string[$strLength]) > 127 ){
				if ( $width + 2 >$length){
					break;
				}
				$strLength += 3;
				$width     += 2;
				$i +=3;
			}
			else{
				$strLength += 1;
				$width     += 1;
				$i +=1;
			}
			$wordLength +=1;
		}
		return mb_substr($string, 0, $wordLength,'utf-8').($len != $strLength ? $etc : '');
	}

	/**
	 * 对字符串进行简单编码，不让搜索蜘蛛识别
	 * @param $text
	 * @return unknown_type
	 */
	public static function encodeText($text) {
		$encoded_text = '';

		for ($i = 0; $i < strlen($text); $i++)
		{
			$char = $text{$i};
			$r = rand(0, 100);

			# roughly 10% raw, 45% hex, 45% dec
			# '@' *must* be encoded. I insist.
			if ($r > 90 && $char != '@')
			{
				$encoded_text .= $char;
			}
			else if ($r < 45)
			{
				$encoded_text .= '&#x'.dechex(ord($char)).';';
			}
			else
			{
				$encoded_text .= '&#'.ord($char).';';
			}
		}

		return $encoded_text;
	}
	
	/**
	 * 检查字符串是不是为空
	 * @param string $str
	 * @return bool
	 */
	public static function is_empty($str) {
		if(!isset($str) || is_null($str) || trim($str) == '' )
			return true;
		return false;
	}
	
	/**
	 * 检查字符串是不是全部是中文
	 * @param $str
	 * @return bool
	 */
	public static function isAllZhChars($str) {		
		if(preg_match('/^[\x{4E00}-\x{9FA5}]+$/u', $str) > 0) {
			return true;
		}
		return false;
	}
}

?>
